package com.kwonder.controller;


//import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/file")
public class ReadFileController {

    @Autowired
    private ResourceLoader resourceLoader;


    @GetMapping("/filmJson")
    public String readFilmJson() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:film.json");
        InputStream inputStream = resource.getInputStream();
        StringBuilder stringBuilder = new StringBuilder();

        // 使用StreamUtils.copyToString方法将输入流内容读取为字符串
        String jsonContent = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
        return jsonContent;

//        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
//            String line;
//            while ((line = reader.readLine()) != null) {
//                stringBuilder.append(line);
//            }
//        }
//        return stringBuilder.toString();
    }
}
